package com.fenger.sys.controller;


import com.fenger.sys.contast.SysConstast;
import com.fenger.sys.domain.User;
import com.fenger.sys.service.UserService;
import com.fenger.sys.utils.*;
import com.fenger.sys.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    private final static String USER_PREFIX = "user/";

    @Autowired
    UserService userService;

    @ResponseBody
    @RequestMapping(USER_PREFIX + "loadAllUser")
    private DataGirdView loadAllUser(UserVo userVo) {
        return this.userService.queryAllUser(userVo);
    }

    @ResponseBody
    @RequestMapping(USER_PREFIX + "loadUserByDeptId")
    private List<User> loadUserByDeptId(UserVo userVo) {
        return this.userService.queryUserByDeptId(userVo.getDeptid());
    }
  @ResponseBody
    @RequestMapping(USER_PREFIX + "loadUserById")
    private Map<String,Object> loadUserById(UserVo userVo) {
        User user = this.userService.queryUserById(userVo.getId());
        Map<String,Object> map = new HashMap<>();
        map.put("user",user);
        return map;
    }

    @ResponseBody
    @RequestMapping(USER_PREFIX + "getUserPinYin")
    private Map<String,Object> getUserPinYin(String name) {
        String s = PinYinUtils.toPinyin(name);
        Map<String,Object> map = new HashMap<>();
        map.put("loginname",s);
        return map;
    }

    @ResponseBody
    @RequestMapping(USER_PREFIX + "addUser")
    private DataGirdView addUser(UserVo userVo) {
        try {
            userVo.setType(SysConstast.USER_NORMAL);
            userVo.setImgpath(SysConstast.SYSTEM_DEFAULT_IMGPATH);

            String salt = RandomUtils.createRandomStringUserUUID();
            userVo.setSalt(salt);
            String pwd = Md5Utils.md5Password(SysConstast.SYSTEM_DEFAULT_PASSWORD,salt);
            userVo.setPwd(pwd);
            this.userService.addUser(userVo);
            return new DataGirdView(SysConstast.ADD_SUCCESS_CODE, SysConstast.ADD_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.ADD_ERROR_CODE, SysConstast.ADD_ERROR_MSG, 0L, null);
        }
    }

    @ResponseBody
    @RequestMapping(USER_PREFIX + "updateUser")
    private DataGirdView updateUser(UserVo userVo) {

        try {
            this.userService.updateUser(userVo);
            return new DataGirdView(SysConstast.UPDATE_SUCCESS_CODE, SysConstast.UPDATE_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.UPDATE_ERROR_CODE, SysConstast.UPDATE_ERROR_MSG, 0L, null);
        }
    }


    @ResponseBody
    @RequestMapping(USER_PREFIX + "updateUserPwd")
    private DataGirdView updateUserPwd(UserVo userVo) {

        try {
            this.userService.updateUserPwd(userVo);
            return new DataGirdView(SysConstast.UPDATE_SUCCESS_CODE, SysConstast.UPDATE_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
//            e.printStackTrace();
            return new DataGirdView(SysConstast.UPDATE_ERROR_CODE, e.getMessage(), 0L, null);
        }

    }


    @ResponseBody
    @RequestMapping(USER_PREFIX + "resetUserPwd")
    private DataGirdView resetUserPwd(UserVo userVo) {

        try {
            String salt = RandomUtils.createRandomStringUserUUID();
            userVo.setSalt(salt);
            String password = Md5Utils.md5Password(SysConstast.SYSTEM_DEFAULT_PASSWORD,salt);
            userVo.setPwd(password);
            this.userService.updateUser(userVo);
            return new DataGirdView(SysConstast.RESET_SUCCESS_CODE, SysConstast.RESET_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.RESET_ERROR_CODE, SysConstast.RESET_ERROR_MSG, 0L, null);
        }
    }

@ResponseBody
    @RequestMapping(USER_PREFIX + "dispatcherUserRole")
    private DataGirdView dispatcherUserRole(UserVo userVo) {

        try {
            Integer[] rids = userVo.getIds();
            Integer id = userVo.getId();
            this.userService.saveUserRole(id,rids);
            return new DataGirdView(SysConstast.DISPATCHER_SUCCESS_CODE, SysConstast.DISPATCHER_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.DELETE_ERROR_CODE, SysConstast.DISPATCHER_ERROR_MSG, 0L, null);
        }
    }

    @ResponseBody
    @RequestMapping(USER_PREFIX + "loadUserMaxOrderNum")
    private Map<String, Object> loadUserMaxOrderNum() {
        Integer orderNum = userService.queryUserMaxOrderNum();
        HashMap<String, Object> map = new HashMap<>();
        map.put("orderNum", orderNum + 1);
        return map;
    }

    @ResponseBody
    @RequestMapping(USER_PREFIX + "deleteUser")
    private DataGirdView deleteUser(UserVo userVo) {

        try {
            this.userService.deleteUser(userVo.getId());
            return new DataGirdView(SysConstast.DELETE_SUCCESS_CODE, SysConstast.DELETE_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.DELETE_ERROR_CODE, SysConstast.DELETE_ERROR_MSG, 0L, null);
        }
    }


    @ResponseBody
    @RequestMapping(USER_PREFIX + "deleteListUser")
    private DataGirdView deleteListUser(@RequestParam(value = "id[]", required = false) String[] id) {
        System.out.println("--------------------");
        System.out.println(id);
        for (int i = 0; i < id.length; i++) {
            String s = id[i];
            System.out.println(s);
        }
        return new DataGirdView(SysConstast.ERROR_CODE, SysConstast.DELETE_ERROR_MSG, 0L, null);
    }
}
